
/*
 * @项目名称: 模板开发
 * @FilePath: /meek/src/utils/obj.js
 * @Author: 1106408264@qq.com
 * @Date: 2017-06-05T10:55:48+08:00
 * @LastEditors: 
 * @LastEditTime: 2021-02-05T15:26:07+08:00
 * Copyright (c) 2018-2022
 */

/**
 * Created by zhangyong on 2017/6/5.
 */

 export const assign = (typeof Object.assign === 'function') ? Object.assign : function(target, var_sources) {
  if (target === undefined || target === null) {
    throw new TypeError('Cannot convert undefined or null to object');
  }

  const output = Object(target);
  for (let i = 1, ii = arguments.length; i < ii; ++i) {
    const source = arguments[i];
    if (source !== undefined && source !== null) {
      for (const key in source) {
        if (source.hasOwnProperty(key)) {
          output[key] = source[key];
        }
      }
    }
  }
  return output;
};

/**
 * Removes all properties from an object.
 * @param {Object} object The object to clear.
 */
export function clear(object) {
  for (const property in object) {
    delete object[property];
  }
}

export default class Obj {

  /**
   * Get a collection from object
   * @param object
   * @returns {Array}
   */
  static getValues (object) {
    const values = []
    for (let property in object) {
      values.push(object[property])
    }
    return values
  }

  /**
   * Clear all properties in object
   * @param object
   */
  static clear (object) {
    for (let property in object) {
      Reflect.deleteProperty(object, property)
    }
  }

  /**
   * Convert an object to a map instance
   * @param obj
   * @returns {Map}
   */
  static objectToMap (obj) {
    let map = new Map()

    for (let ke of Object.keys(obj)) {
      map.set(ke, obj[ke])
    }

    return map
  }

  /**
   *
   * Convert a map object to object
   *
   * @static
   * @method mapToObject
   * @param map {Map} 传入的 Map 的对象
   * @returns {Object} 装换成后的 Object 的对象
   */
  static mapToObject (map) {
    // 合法性判断
    if (map === undefined || map === null) {
      return {}
    }

    let object = {}
    map.forEach( (value, key) => {
      object[key] = value
    })

    return object
  }


}

